package com.mondo.financialwallet.presentation.ui.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.common.ui.BaseActivity;
import com.example.common.utils.TimeCount;
import com.mondo.financialwallet.R;
import com.mondo.financialwallet.data.DataRepository;
import com.mondo.financialwallet.data.params.ResetPasswordParams;
import com.mondo.financialwallet.presentation.event.LogOutEvent;
import com.mondo.financialwallet.presentation.utils.ActivityTitleBarHelper;
import com.mondo.financialwallet.presentation.utils.Toasts;

import org.greenrobot.eventbus.Subscribe;

import butterknife.Bind;

/**
 * 获取验证码
 */
public class GetVerificationCodeActivity extends BaseActivity {

    private static final String EXTRA_MOBILE_NUMBER = "MOBILE_NUMBER",
            EXTRA_PREFIX = "PREFIX",
            EXTRA_PAGE_TYPE = "PAGE_TYPE";


    public static void alterPassword(Context context, String mobileNumber, String prefix) {
        context.startActivity(new Intent(context, GetVerificationCodeActivity.class)
                .putExtra(EXTRA_MOBILE_NUMBER, mobileNumber)
                .putExtra(EXTRA_PREFIX, prefix)
                .putExtra(EXTRA_PAGE_TYPE, "ALTER")
        );
    }

    public static void launch(Context context, String mobileNumber, String prefix) {
        context.startActivity(
                new Intent(context, GetVerificationCodeActivity.class)
                        .putExtra(EXTRA_MOBILE_NUMBER, mobileNumber)
                        .putExtra(EXTRA_PREFIX, prefix)
                        .putExtra(EXTRA_PAGE_TYPE, "FORGET")
        );
    }

    @Bind(R.id.next_view) View mNextButton;
    @Bind(R.id.hint_text_view) TextView mHintTextView;
    @Bind(R.id.timer_text_view) TextView mTimerTextView;
    @Bind(R.id.message_code_edit) EditText mMessageCodeEdit;

    private String mPageType;
    private ProgressDialog mSendingProgressDialog;

    @Override
    protected boolean useEventBus() {
        return true;
    }

    @Override
    protected int getBackgroundColor() {
        return Color.WHITE;
    }

    @Override
    protected void setStatusBar() {
        ActivityTitleBarHelper.setWhiteDarkColorStatus(this);
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_get_verification_code;
    }

    @Override
    protected void initActions() {
        ActivityTitleBarHelper.init(this, R.string.title_get_verification_code);

        mPageType = getIntent().getStringExtra(EXTRA_PAGE_TYPE);

        // 电话
        String mobileNumber = getIntent().getStringExtra(EXTRA_MOBILE_NUMBER);

        // 文字提示
        mHintTextView.setText(Html.fromHtml(String.format(getString(
                R.string.text_get_verification_code_hint), mobileNumber)));

        // 倒计时
        if (!"ALTER".equals(mPageType)){
            new TimeCount(mTimerTextView).start();
        }

        // 重新获取验证码
        mTimerTextView.setOnClickListener(v -> sendMessageCode(mobileNumber));

        // 验证信息
        mNextButton.setOnClickListener(v -> checkResult(
                mobileNumber,
                mMessageCodeEdit.getText().toString()
        ));
    }

    private void checkResult(String mobileNumber, String code) {
        if (TextUtils.isEmpty(code)) {
            Toasts.warning(R.string.message_please_auth_code);
            return;
        }

        DataRepository.get().checkMessageCodeResult(mobileNumber, code, result -> {
            if (result.isSuccess()) {
                String token = result.getData().getToken();
                if ("ALTER".equals(mPageType)) {
                    startActivity(new Intent(this, BindNewMobileNumberActivity.class));
                } else {
                    ResetPasswordActivity.launch(
                            this, new ResetPasswordParams(token, code, mobileNumber));
                }

            } else {
                Toasts.failure(result.getMsg());
            }
        });
    }

    private void sendMessageCode(String mobileNumber) {
        if (mSendingProgressDialog == null) {

            mSendingProgressDialog = ProgressDialog.show(this, null,
                    getString(R.string.message_sending_sms_verification_code));

            DataRepository.get().sendMessageByUser(mobileNumber, result -> {
                mSendingProgressDialog.dismiss();
                mSendingProgressDialog = null;

                if (result.isSuccess()) {
                    new TimeCount(mTimerTextView).start();
                } else {
                    Toasts.failure(result.getMsg());
                }
            });
        }
    }

    @Subscribe
    public void logoutEvent(LogOutEvent event){
        finish();
    }
}
